<template>
    <form-create style="height:calc(100vh - 103px)" v-model="formData"  :rule="rule"
        :option="options"></form-create>
</template>

<script lang="ts" setup>
import { ref,defineProps,onMounted,defineExpose } from 'vue';
import formCreate from "@form-create/element-ui";
import { getNodeForm } from '@/api/workflow/innerform';
import { message, notification } from 'ant-design-vue';
import { save,getData } from '@/api/form/onlineform';

const formData = ref({});
// 规则
const rule = ref()
const options = ref();

const props = defineProps({
    params: Object,
    pageParams: Object,
});

const Save = () => {
    save(JSON.stringify(formData.value)).then((res: any) => {
        message.success(res.data.Message); 
    }).catch((err) => {
        console.log(err||'保存表单失败');
        message.error(err.message||'保存表单失败');
    })
}

onMounted(() => { 
    formData.value.FormId = props.pageParams.formId;
    formData.value.BusinessKey = props.params.processInstanceId;
    getNodeForm({Id:props.pageParams.id}).then((res: any) => {
        let formObj =JSON.parse(res.data.Data.FormContent);
        rule.value = JSON.parse(formObj.rule);
        options.value = JSON.parse(formObj.options);
    }).catch((err) => {
        console.log(err||'获取表单失败');
        message.error(err.message||'获取表单失败');
    })
    getData({formId:props.pageParams.formId,businessKey:props.params.processInstanceId}).then((res: any) => {
        formData.value = JSON.parse(res.data.Data); 
    }).catch((err) => {
        console.log(err||'获取表单数据失败');
        message.error(err.message||'获取表单数据失败');
    })
})

// 导出钩子方法，以便外部调用
defineExpose({
    Save,
});


</script>